/*
 * PFactoresExtras.java
 *
 * Created on 13 de marzo de 2007, 05:47 PM
 */

package ig.metrica.ui.matrizcorrelacion;

import ig.metrica.riesgo.FactorRiesgoMatrizCorrelacionMetrica;
import ig.util.MyTableModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author  jsanchez
 */
public class PFactoresExtras extends javax.swing.JPanel implements IPanel{
    public Hashtable<IPanel,Integer> oyentes=new Hashtable<IPanel,Integer>();
    /** Creates new form PFactoresExtras */
    public PFactoresExtras() {
        initComponents();
        this.TFactorExtar.getTableHeader().setReorderingAllowed(false);
        this.TFactorExtar.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PMAddFactor = new javax.swing.JPopupMenu();
        MAdd = new javax.swing.JMenuItem();
        MRem = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        TFactorExtar = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();

        MAdd.setText(org.openide.util.NbBundle.getMessage(PFactoresExtras.class, "PFactoresExtras.MAdd.text")); // NOI18N
        MAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MAddActionPerformed(evt);
            }
        });
        PMAddFactor.add(MAdd);

        MRem.setText(org.openide.util.NbBundle.getMessage(PFactoresExtras.class, "PFactoresExtras.MRem.text")); // NOI18N
        MRem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MRemActionPerformed(evt);
            }
        });
        PMAddFactor.add(MRem);

        jScrollPane1.setComponentPopupMenu(PMAddFactor);

        TFactorExtar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        TFactorExtar.setComponentPopupMenu(PMAddFactor);
        jScrollPane1.setViewportView(TFactorExtar);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(org.openide.util.NbBundle.getMessage(PFactoresExtras.class, "PFactoresExtras.jLabel2.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                .add(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void MAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MAddActionPerformed
        // TODO add your handling code here:
        Object[][] data=((MyTableModel)this.TFactorExtar.getModel()).getData();
        Object[][] dataClon=data.clone();
        data=new Object[dataClon.length+1][2];
        for (int i = 0; i < dataClon.length; i++) {
            data[i][0]=dataClon[i][0];
            data[i][1]=dataClon[i][1];
        }
        data[dataClon.length][0]="";
        data[dataClon.length][1]=0.0;
        //((MyTableModel)this.TFactorExtar.getModel()).setData(data);
        this.TFactorExtar.setModel(new MyTableModel(data,new String [] {"Nombre","Cambio Esperado"},new boolean [] {true,true}));
        this.TFactorExtar.updateUI();

    }//GEN-LAST:event_MAddActionPerformed

    private void MRemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MRemActionPerformed

        Object[][] data=((MyTableModel)this.TFactorExtar.getModel()).getData();
        int rox=TFactorExtar.getSelectedRow();
        Object[][] dataClon=data.clone();
        data=new Object[dataClon.length-1][2];
        int j=0;
        for (int i = 0; i < dataClon.length; i++) {
            if(rox!=i){
                data[j][0]=dataClon[i][0];
                data[j++][1]=dataClon[i][1];
            }
        }
        this.TFactorExtar.setModel(new MyTableModel(data,new String [] {"Nombre","Cambio Esperado"},new boolean [] {true,true}));
        this.TFactorExtar.updateUI();
    }//GEN-LAST:event_MRemActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MAdd;
    private javax.swing.JMenuItem MRem;
    private javax.swing.JPopupMenu PMAddFactor;
    private javax.swing.JTable TFactorExtar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    Hashtable ht=null;
    Collection<FactorRiesgoMatrizCorrelacionMetrica> c=null;
    //@Override
    public void update(Object obj) {
        ht=(Hashtable) obj;
        if(ht.get("FACTORES_EXTRAS")!=null){
            c=(Collection)ht.get("FACTORES_EXTRAS");
            Object[][] data=new Object[c.size()][2];
            int i=0;
            for (FactorRiesgoMatrizCorrelacionMetrica elem : (Collection<FactorRiesgoMatrizCorrelacionMetrica >)c) {
                data[i++][0]=elem.toString();
                data[i-1][1]=elem.getCambioEsperado();
            }
            this.TFactorExtar.setModel(new MyTableModel(data,new String [] {"Nombre","Cambio Esperado"},new boolean [] {true,true}));
        }
    }
    public Collection getData(){
        Collection cc=new ArrayList();
        Object[][] data=((MyTableModel)this.TFactorExtar.getModel()).getData();
        for (int i = 0; i < data.length; i++) {
            if(!data[i][0].toString().trim().equals("") && !data[i][1].toString().trim().equals("")){
                FactorRiesgoMatrizCorrelacionMetrica frmcm=new FactorRiesgoMatrizCorrelacionMetrica(data[i][0].toString().trim(),Double.valueOf(data[i][1].toString().trim()),0,-1);
                frmcm.setNuevo(true);
                for (FactorRiesgoMatrizCorrelacionMetrica fr : c) {
                    if(fr.getNombre().equalsIgnoreCase(frmcm.getNombre())){
                        frmcm.setId(fr.getId());
                        frmcm.setNuevo(false);
                    }
                }
                cc.add(frmcm);
            }
        }
        return cc;
    }
    //@Override
    public String getTitle() {
        return this.getName();
    }
    
    //@Override
    public void addOyente(IPanel oyente, Integer integer) {
        oyentes.put(oyente,integer);
    }
    
    //@Override
    public void setTitle(String title) {
        this.setName(title);
    }
    
    //@Override
    public void notificarSiguiente()throws Exception {
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(0))
                ipanel.update(ht);
        }
    }
    
    //@Override
    public void notificarAnterior()throws Exception {
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(1))
                ipanel.update(ht);
        }
    }
    
}
